/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2012-8-29         xingkong207
 */

package com.skyworth.tvos.skydom;

import com.skyworth.tvos.ipcservice.ServiceDispatch.SkyPackage;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.ArrayList;

/**
 * @author Wei Li
 * @version TODO (write something)
 * @ClassName SkyDocument
 * @Description TODO (write something)
 * @date 2012-8-29
 */
public class SkyServiceDocument extends SkyBaseDocment {
    public SkyServiceDocument() {
        super();
    }

    public SkyServiceDocument(String filename) {
        super(filename);
    }

    /* (non-Javadoc)
     * @see com.skyworth.study.SkyBaseDocment#getItemsByName(java.lang.String)
     */
    @Override
    public ArrayList<Object> getItemsByName(String name) {
        // TODO Auto-generated method stub
        NodeList nodes = mDoc.getElementsByTagName(name);
        int index = 0;
        SkyPackage item = null;
        ArrayList<Object> pkgs = new ArrayList<Object>();
        while (index < nodes.getLength()) {
            item = new SkyPackage();
            Node node = nodes.item(index);
            item.serviceName = node.getAttributes().getNamedItem("name").getTextContent();
            item.packageName = node.getAttributes().getNamedItem("package").getTextContent();
            item.delay = Float.valueOf(node.getAttributes().getNamedItem("delay").getTextContent());
            Node checkTimesNode = node.getAttributes().getNamedItem("maxchecktimes");
            if (checkTimesNode != null) {
                if (checkTimesNode.getTextContent() != null && !checkTimesNode.getTextContent().equals("")) {
                    try {
                        item.maxCheckTimes = Integer.valueOf(checkTimesNode.getTextContent());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            pkgs.add(item);

            index++;
        }
        return pkgs;
    }
}
